﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OttawaTourMap.Models;
using System.Windows.Navigation;
using Expression.Blend.SampleData.ottawaSampleDataSource;
using OttawaTourMap.Resources.Localization;
using System.Collections.ObjectModel;

namespace OttawaTourMap
{
    public partial class StopDetailsPage_new : PhoneApplicationPage
    {
       
        private string stopDescription;
        private stop selectedStop = null;
       
        //Create a List to store all the image models
        public List<ImagesModel> listOfImageCollections = null;
        public StopDetailsPage_new()
        {
            InitializeComponent();
            //LocalizeStrings();
            stopDetailsModel = new StopDetailsModel();
           
            listOfImageCollections = new List<ImagesModel>();
        }
        private void LocalizeStrings()
        {
            StopDetailPanorama.Title = Labels.Stop_Details;
            Points_Item.Header = Labels.Points;
            Description_Item.Header = Labels.Description;
        }

        //This is the data context for the Stop details list - Collection of Point Of Interests
        public StopDetailsModel stopDetailsModel = null;


        //over ride the on navigate to to extract the details of the tour page
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string s = "";
            int stopIndex = 0;
            string tourId = null;
            tour selectedTour = null;
            var toursSource = (toursList)App.Current.Resources["OttawaSampleDataSource"];

            if (NavigationContext.QueryString.TryGetValue("tourIndex", out s))
            {
                tourId = (s);

                //Get the selected tour inforamtion from the id
                //Iterate through the collection

                foreach (var tour in toursSource.tourCollection)
                {
                    if (tour.id.Equals(tourId))
                    {
                        selectedTour = tour;
                        break;
                    }
                }


            }

           
            if (NavigationContext.QueryString.TryGetValue("stopIndex", out s))
            {
                stopIndex = int.Parse(s);
                selectedStop = selectedTour.stopCollection[stopIndex];

                //Get all the Point of Interest Collection
                //TODO : Change this code to handle collection
                foreach (var poi in selectedStop.pointOfInterestCollection)
                {
                    stopDetailsModel.addToCollection(new StopDetails(poi.title, poi.ThumbNailURI));
                    
                    //Create image model
                    ImagesModel imagesModel = new ImagesModel();
                    foreach (var image in poi.imageCollection)
                    {
                        imagesModel.addImageToCollection(new ImageURI(image.imageURI, image.title));
                    }
                    listOfImageCollections.Add(imagesModel);
                    
                }
               
                //Store all the images for the point of interest 


            }

            //set the datacontext
            IntrestListBox.DataContext = stopDetailsModel;

            //Show the Description of First point of interest and on list box selection change it will automatically be updated
            stopDescription = selectedStop.pointOfInterestCollection[0].longDescription;
            StopDescriptionTextBlock.Text = stopDescription;

            picturesGrid.DataContext = listOfImageCollections.ElementAt(0);
           // StopDetailPanorama.Title = Labels.Stop_Details;

        }

        

        private void StopListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox stopListBox = sender as ListBox;

            int index = stopListBox.SelectedIndex;

            picturesGrid.DataContext = listOfImageCollections.ElementAt(index);

            stopDescription = selectedStop.pointOfInterestCollection[index].longDescription;

            //Get the index of the point of interest and then update the description

        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}